import cv2
import base64
import time
from config import timeit

@timeit('red')
def main(imgName):
    cap = cv2.VideoCapture(0)  # Open the default camera
    if not cap.isOpened():
        print("Cannot open camera")
        return ""

    time.sleep(2)  # Wait for the camera to adjust

    ret, frame = cap.read()
    if not ret:
        print("Cannot read from camera")
        cap.release()
        return ""

    # Save the captured image to a local file
    image_path = imgName
    cv2.imwrite(image_path, frame)
    cap.release()
    # Read the saved image file and convert to Base64
    with open(image_path, 'rb') as f:
        base64str = base64.b64encode(f.read()).decode('utf-8')
    return base64str
def fromImg(imgName):
    with open(imgName, 'rb') as f:
        base64str = base64.b64encode(f.read()).decode('utf-8')
    return base64str
if __name__ == "__main__":
    base64str = main("labelrecog.png")
    if base64str:
        print(base64str[0:10])
